<template>
  <page-header-wrapper :title="false">
    <a-card title="申请主体列表">
      <div class="table-page-search-wrapper">
        <a-form-model
          layout="inline"
          :model="queryParam"
        >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="主体名称">
                <a-input v-model="queryParam['filter[subject]']" placeholder="请输入主体名称" :allowClear="true" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="主体类型">
                <a-select v-model="queryParam['filter[type]']" placeholder="请选择主体类型" :allowClear="true">
                  <a-select-option v-for="(item,index) in typeOpts" :key="index" :value="index">{{item}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="联系电话">
                  <a-input v-model="queryParam['filter[phone]']" placeholder="请输入联系电话" :allowClear="true" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="身份证号">
                  <a-input v-model="queryParam['filter[id_card]']" placeholder="请输入身份证号" :allowClear="true" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="资质编号">
                  <a-input v-model="queryParam['filter[license_num]']" placeholder="请输入资质编号" :allowClear="true" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="添加时间">
                  <a-range-picker v-model="queryParam['created_at_start']" @change="onChange" />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          size="middle"
          ref="table"
          :row-key="(record) => record.id"
          :columns="subjectColumns"
          :data="subjectData"
        >
          <template slot="title">
            <div style="color: #ff0000;font-weight: bold">申请主体总数：{{count}}</div>
          </template>
          <div
            slot="filterDropdown"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-input
              v-ant-ref="c => (searchInput = c)"
              :placeholder="`搜索${column.title}`"
              :value="selectedKeys[0]"
              style="width: 188px; margin-bottom: 8px; display: block;"
              @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
              @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterSelect"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in typeOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterRange"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-range-picker
              style="width: 228px; margin-bottom: 8px; display: block;"
              :value="createValue"
              @change="onChangeDate"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 110px; margin-right: 8px"
              @click="() => handleRange(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 110px" @click="() => rangeReset(clearFilters)">
              重置
            </a-button>
          </div>
          <template slot="license_file" slot-scope="text,record">
            <div v-if="text != ''">
              <img width="60" :src="text" @click="handlePreview(text)">
            </div>
            <div v-else>
              <img style="margin-right:5px" width="60" :src="record.id_card_front" @click="handlePreview(record.id_card_front)">
              <img style="margin-right:5px" width="60" :src="record.id_card_reverse" @click="handlePreview(record.id_card_reverse)">
              <img v-if="record.personal_license_file != ''" width="60" :src="record.personal_license_file" @click="handlePreview(record.personal_license_file)">
            </div>
          </template>
<!--          <template slot="id_card_front" slot-scope="text">-->
<!--            <a @click="handlePreview(text)" v-if="text != ''">-->
<!--              <img :src="text" width="30" height="30" />-->
<!--            </a>-->
<!--          </template>-->
<!--          <template slot="id_card_reverse" slot-scope="text">-->
<!--            <a @click="handlePreview(text)" v-if="text != ''">-->
<!--              <img :src="text" width="30" height="30" />-->
<!--            </a>-->
<!--          </template>-->
<!--          <template slot="license_file" slot-scope="text">-->
<!--            <a @click="handlePreview(text)" v-if="text != ''">-->
<!--              <img :src="text" width="30" height="30" />-->
<!--            </a>-->
<!--          </template>-->
<!--          <template slot="personal_license_file" slot-scope="text">-->
<!--            <a @click="handlePreview(text)" v-if="text != ''">-->
<!--              <img :src="text" width="30" height="30" />-->
<!--            </a>-->
<!--          </template>-->
        </s-table>
      </div>
      <a-modal v-model="previewVisible" :footer="null" @cancel="handleCancel">
        <img alt="example" style="width: 100%" :src="previewImage" />
      </a-modal>
    </a-card>
  </page-header-wrapper>
</template>

<script>
  import { STable, Ellipsis } from '@/components'
  import customerColumns from '@/columns/customer'
  import { getPublicSubject } from '@/api/customer'

  export default {
    name: 'ApplicationSubject',
    components: { STable, Ellipsis },
    data () {
      return {
        queryParam: {},
        advanced: true,
        subjectColumns: customerColumns.subject,
        subjectData: parameters => {
          return getPublicSubject(Object.assign(parameters, this.queryParam)).then(res => {
            this.count = res.result.total_count
            this.typeOpts = res.result.typeOpts
            return res.result
          })
        },
        previewVisible: false,
        previewImage: '',
        createValue: [],
        typeOpts: {},
        count: ''
      }
    },
    methods: {
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      resetForm () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      onChange (date, dateString) {
        this.queryParam['filter[created_at_start]'] = dateString[0]
        this.queryParam['filter[created_at_end]'] = dateString[1]
      },
      async handlePreview (file) {
        const app = this
        app.previewImage = file
        app.previewVisible = true
      },
      handleCancel ({ fileList }) {
        this.fileList = fileList
      },
      handleSelect (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleSearch (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleReset (clearFilters, dataIndex) {
        clearFilters()
        delete this.queryParam['filter[' + dataIndex + ']']
      },
      onChangeDate (date, dateString) {
        this.createValue = date
        this.queryParam['filter[created_at_start]'] = dateString[0]
        this.queryParam['filter[created_at_end]'] = dateString[1]
      },
      handleRange (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.$refs.table.refresh(true)
      },
      rangeReset (clearFilters) {
        clearFilters()
        this.createValue = []
        delete this.queryParam['filter[created_at_start]']
        delete this.queryParam['filter[created_at_end]']
        this.$refs.table.refresh(true)
      }
    }
  }
</script>

<style scoped>

</style>
